﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SMM.MainForm
{
    public partial class CargoForm : Form
    {
        String id="";
        sqlHelper helper = new sqlHelper();
        public CargoForm(String id)
        {
            InitializeComponent();
            loadDGV();
            this.id = id;
            label3.Text = "补货员ID：" + id;
            barTxt.Text = "";
            cargoLab.Text = "";
            barTxt.Select();
        }

        private void addBtn_Click(object sender, EventArgs e)
        {
            if (helper.cargoExist(barTxt.Text))
            {
                if (helper.addCargoNum(barTxt.Text, int.Parse(addNumTxt.Text.Trim())))
                {
                    MessageBox.Show("货物库存添加成功！", "成功");
                    barTxt.Text = "";
                    cargoLab.Text = "";
                    addNumTxt.Text = "";
                    loadDGV();
                }else
                    MessageBox.Show("货物添加失败！", "错误");
            }
            else {
                MessageBox.Show("请输入补货的商品条形码！","错误");
            }
        }

        private void barTxt_Leave(object sender, EventArgs e)
        {
            if (helper.cargoExist(barTxt.Text))
            {
                cargoLab.Text = helper.getCargoInfoByBar(barTxt.Text, "Name");
                nowNumLab.Text = "当前库存量："+helper.getCargoInfoByBar(barTxt.Text, "Stock");
            }
        }

        private void loadDGV() {
            dgvAllCargo.DataSource = helper.getTable("cargo").Tables[0];
            dgvAllCargo.Columns[0].HeaderText = "货物ID";
            dgvAllCargo.Columns[1].HeaderText = "货物条形码";
            dgvAllCargo.Columns[2].HeaderText = "货物名称";
            dgvAllCargo.Columns[3].HeaderText = "货物类型";
            dgvAllCargo.Columns[4].HeaderText = "货物价格";
            dgvAllCargo.Columns[5].HeaderText = "货物库存";
            dgvAllCargo.Columns[6].HeaderText = "货物预警库存";

            dgvNeedCargo.DataSource = helper.getTable().Tables[0];
            dgvNeedCargo.Columns[0].HeaderText = "货物ID";
            dgvNeedCargo.Columns[1].HeaderText = "货物条形码";
            dgvNeedCargo.Columns[2].HeaderText = "货物名称";
            dgvNeedCargo.Columns[3].HeaderText = "货物类型";
            dgvNeedCargo.Columns[4].HeaderText = "货物价格";
            dgvNeedCargo.Columns[5].HeaderText = "货物库存";
            dgvNeedCargo.Columns[6].HeaderText = "货物预警库存";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String str = "\t普通超市\r\n\t补货清单\r\n补货员："+id+"\r\n打印时间："+ DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")
                +"\r\n条形码\t名称\t当前库存\t最低库存\r\n";
            for (int i = 0; i < dgvNeedCargo.RowCount; i++)
            {
                for (int j = 0; j < dgvNeedCargo.ColumnCount; j++)
                {
                    if (j == 0||j==3||j==4) continue;
                    str += Convert.ToString(dgvNeedCargo.Rows[i].Cells[j].Value).Trim() + "\t";
                }
                str += "\r\n";
            }
            printList(str);
            MessageBox.Show("打印成功！","成功");
        }

        private void printList(String str)
        {
            String fileName = "D:\\补货" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".txt";
            if (!File.Exists(fileName))
            {
                FileStream fs1 = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs1);
                sw.WriteLine(str);//开始写入值
                sw.Close();
                fs1.Close();
            }
            else
            {
                FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Write);
                StreamWriter sr = new StreamWriter(fs);
                sr.WriteLine(str);//开始写入值
                sr.Close();
                fs.Close();
            }
        }
    }
}
